var UserDataListView = Base.extend({

    _updateTimeout: null,
    _model: null,
    _userInfoContentsContainer: null,
    _pleloaderBlock: null,

    constructor: function()
    {
        this._model = UserDataListModel.getInstance();
        this._model.addListener(UserDataListModel.event.UPDATED, this, this._onModelUpdated);
        this._pleloaderBlock = $('#pleloaderBlock');
        this._userInfoContentsContainer = $('#userInfoContents');
        this._update();
    },

    _update: function(data)
    {
        this._pleloaderBlock.show();
        if (!data)
        {
            data = {};
        }
        $.ajax({
            type: 'POST',
            data: data,
            url: '/lab3/user_info_management.php',
            success: handler(this, '_onUpdateSuccess')
        });
    },

    _onUpdateSuccess: function(response)
    {
        this._pleloaderBlock.hide();
        this._model.updateFromJson(response);
    },

    _onModelUpdated: function()
    {
        var userDataList = this._model.getUserDataList();
        var userInfoTableContents = this._prepareUserInfoTableContents(userDataList);
        this._userInfoContentsContainer.html(userInfoTableContents);
        this._userInfoContentsContainer.find('a').bind('click', handler(this, '_onDeleteButtonClick'));

        if (this._updateTimeout !== null)
        {
            clearTimeout(this._updateTimeout);
        }
        this._updateTimeout = setTimeout(handler(this, '_update'), 8000);
    },

    _onDeleteButtonClick: function(event)
    {
        event.preventDefault();
        var deletedItemId = event.target.tabIndex;
        this._update({id: deletedItemId});
    },

    _prepareUserInfoTableContents: function(userDataList)
    {
        var userInfoTableString = '';
        for (var i in userDataList)
        {
            var userData = userDataList[i];
            userInfoTableString += '<tr>';
            userInfoTableString += '<td>' + userData.name + '</td>';
            var sexString = (userData.sex == 0) ? 'Мужской' : 'Женский';
            userInfoTableString += '<td>' + sexString + '</td>';
            var notifyString = (userData.notify) ? 'Уведомить' : 'Не уведомлять';
            userInfoTableString += '<td>' + notifyString + '</td>';
            userInfoTableString += '<td>' + userData.comment + '</td>';
            userInfoTableString += '<td><a tabindex="' + userData.id + '" href="javascript:void(0)" title="Удалить">Удалить запись</a></td></tr>';
        }
        return userInfoTableString;
    }

});
